import {
    Message
} from 'element-ui'
import axios from 'axios';

var ajax = axios.create({
    baseURL: process.env.BASE_URL, //测试
    withCredentials: true, //cookie
    crossDomain: true //跨域
});

//添加一个请求拦截器
ajax.interceptors.request.use(function(config) {
    //在请求发出之前进行一些操作
    return config;
}, function(err) {
    console.error(err);
    Message.error({
        message: '请求发生错误,请稍后再试!',
        type: 'error'
    });
    //Do something with request error
    return Promise.reject(err);
});
//添加一个响应拦截器
ajax.interceptors.response.use(function(res) {
    return (res.data.data?res.data.data:res.data.status);
}, function(err) {
    console.error(err);
    Message.error({
        message: '响应发生错误,请稍后再试!',
        type: 'error'
    });
    //Do something with response error
    return Promise.reject(err);
})


export const literaryWorksList = params => {
    return ajax.get('literaryWorks', {params : params});
}
export const getLiteraryWorks = id => {
    return ajax.get('literaryWorks/' + id);
}
export const literaryWorksAdd = data => {
    return ajax.post('literaryWorks', data);
}
export const literaryWorksUpdate = data => {
    return ajax.put('literaryWorks/' + data.literaryWorksId, data);
}
export const literaryWorksDelete = id => {
    return ajax.delete('literaryWorks/' + id);
}

//管理员登录
export const userLogin = params => {
    return ajax.post('user/login', params);
};
//管理员更改密码
export const updatePwd = params => {
    return ajax.post('user/upwd', params);
};
//管理员列表
export const getUserLists = params => {
    return ajax.get('user/list', params);
};
//新增管理员
export const addUser = params => {
    return ajax.post('user', params);
};
//删除管理员
export const deleteUserById = userId => {
    return ajax.delete('user/' + userId);
};
//修改管理员
export const updateUserById = (userId, params) => {
    return ajax.put('user/user/' + userId, params);
};

//文件管理
export const getFileLists = params => {
    return ajax.get('picture', params);
}
export const addFile = params => {
    return ajax.post('picture', params);
}
export const deleteFileById = pictureId => {
    return ajax.delete('picture/' + pictureId);
}
export const updateFileById = params => {
    return ajax.put('picture/' + params.pictureId, params);
}

//Storage
export const getStorageLists = params => {
    return ajax.get('storage/all', params);
}

export const getStorageBykey = key => {
	return ajax.get('storage/'+ key ,key);
}
export const addStorages = params => {
    return ajax.post('storage', params);
}
export const deleteStorageByKey = key => {
    return ajax.delete('storage/' + key);
}
export const updateStorageByKey = params => {
    return ajax.put('storage/' + params.key, params);
}


//文件上传 前台文件需要设置一个path属性
export const uploadFiles = params => {
    return ajax.post('upload' + params.path, params);
};